/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.BorderLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.event.*;

import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
import java.awt.Toolkit;
import java.awt.event.WindowListener;
import system.*;

/**
 *
 * @author Thommie
 */
public class LeftPanel extends JPanel {

    private JList movieList;           // The movies
    private JList favList;           // The movies
    private JList topList;           // The movies
    private Movie selectedMovie;  // The selected movie
    private JScrollPane scrollPaneMovieList;    // A scroll pane
    private JScrollPane scrollPaneFavList;    // A scroll pane
    private JScrollPane scrollPaneTopList;    // A scroll pane
    // The following array holds the values that will
    // be displayed in the monthList list component.
    private MovieHandler movies;
    private GradeHandler gh;
    private UserHandler uH;
    private MyFrame f;
    private RightPanel rightPanel;
    private LowerPanel lP;
    private boolean init = true;
    private DefaultListModel mFav;
    private DefaultListModel mTop;

    public LeftPanel(MovieHandler movies, MyFrame frame, RightPanel rightPanel, LowerPanel lowerPanel, GradeHandler gh, UserHandler uH) {
        super(new GridLayout(1, 1));
        selectedMovie = movies.getMovieByTitle(movies.getTitleList()[0]);
        this.rightPanel = rightPanel;
        rightPanel.getUpperPanel().setLeftPanel(this);
        this.movies = movies;
        this.f = frame;
        this.gh = gh;
        this.uH = uH;

        this.lP = lowerPanel;

        JTabbedPane tabbedPane = new JTabbedPane();


        JComponent movieTabPanel = new JPanel();

        tabbedPane.addTab("Movies", null, movieTabPanel,
                "Movies");


        JComponent favTabPanel = new JPanel();
        tabbedPane.addTab("Favourites", null, favTabPanel,
                "Favourites");

        JComponent topTabPanel = new JPanel();
        tabbedPane.addTab("Top-10", null, topTabPanel,
                "Top-10");

        //Add the tabbed pane to this panel.
        add(tabbedPane);

        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        this.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));

        movieList = new JList(movies.getTitleList());
        movieList.setSelectionBackground(Color.GREEN);
        // Set the selection mode to single selection.
        movieList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // Register the list selection listener.
        movieList.addListSelectionListener(new ListListener());

        mFav = new DefaultListModel();
        String[] temp = movies.getTitleListFavourites(movies.getCurrentUserID());
        for (int i = 0; i < temp.length; i++) {
            mFav.addElement(temp[i]);
        }
        favList = new JList(mFav);
        favList.setSelectionBackground(Color.PINK);
        // Set the selection mode to single selection.
        favList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // Register the list selection listener.
        favList.addListSelectionListener(new ListListener());

        mTop = new DefaultListModel();
        temp = movies.getTop10List();
        for (int i = 0; i < temp.length; i++) {
            mTop.addElement(temp[i]);
        }
        topList = new JList(mTop);
        topList.setSelectionBackground(Color.orange);
        // Set the selection mode to single selection.
        topList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // Register the list selection listener.
        topList.addListSelectionListener(new ListListener());



        this.addComponentListener(new ComponentListener());

        // Add the list to a scroll pane.
        scrollPaneMovieList = new JScrollPane(movieList);
        scrollPaneMovieList.setPreferredSize(new Dimension(200, f.getHeight() - 100));
        add(scrollPaneMovieList, BorderLayout.CENTER);
        movieTabPanel.add(scrollPaneMovieList);
        // Add the list to a scroll pane.
        scrollPaneFavList = new JScrollPane(favList);
        scrollPaneFavList.setPreferredSize(new Dimension(200, f.getHeight() - 100));
        add(scrollPaneFavList, BorderLayout.CENTER);
        favTabPanel.add(scrollPaneFavList);
        // Add the list to a scroll pane.
        scrollPaneTopList = new JScrollPane(topList);
        scrollPaneTopList.setPreferredSize(new Dimension(200, f.getHeight() - 100));
        add(scrollPaneTopList, BorderLayout.CENTER);
        topTabPanel.add(scrollPaneTopList);

        if (init) {
            String selection = movies.getTitleList()[0];
            selectedMovie = movies.getMovieByTitle(selection);
            movies.setSelectedMovie(selectedMovie);
            rightPanel.getUpperPanel().updateIMG(selectedMovie.getImgURL());
            rightPanel.getUpperPanel().getMovieInfo().setText(movies.getMovieInfo(selectedMovie.getMovieID()));
            rightPanel.getUpperPanel().getPlot().setText(movies.getMoviePlot(selectedMovie.getMovieID()));
            rightPanel.getUpperPanel().updateRadioButtons();

            lP.setTextArea();
            init = false;
        }


    }

    public void addFavoruteAndUpdateList(String title) {
        System.out.println("addFavoruteAndUpdateList()");
        String[] temp = movies.getTitleListFavourites(movies.getCurrentUserID());


        for (int i = 0; i < temp.length; i++) {
            if (title.equals(temp[i])) {
                System.out.println("Lägger till: "+temp[i]);
                mFav.add(i, temp[i]);

            }
        }
    }

    public void removeFavoruteAndUpdateList(String title) {
        
        System.out.println("removeFavoruteAndUpdateList()");

        for (int i = 0; i < mFav.getSize(); i++) {
            if (title.equals(mFav.getElementAt(i))) {
                System.out.println("tar bort: "+mFav.getElementAt(i));
                mFav.remove(i);
                //scrollPaneFavList.repaint();

            }
        }
    }

    public void updateTop10List(){
        mTop.removeAllElements();
        String[] temp = movies.getTop10List();
        for (int i = 0; i < temp.length; i++) {
            mTop.addElement(temp[i]);
        }

    }

    public Movie getSelectedMovie() {
        return selectedMovie;
    }

    private class ComponentListener implements java.awt.event.ComponentListener {

        public void componentResized(ComponentEvent e) {
            scrollPaneMovieList.setPreferredSize(new Dimension(200, f.getHeight() - 100));
            scrollPaneMovieList.revalidate();
            scrollPaneFavList.setPreferredSize(new Dimension(200, f.getHeight() - 100));
            scrollPaneFavList.revalidate();
            scrollPaneTopList.setPreferredSize(new Dimension(200, f.getHeight() - 100));
            scrollPaneTopList.revalidate();


        }

        public void componentMoved(ComponentEvent e) {
        }

        public void componentShown(ComponentEvent e) {
        }

        public void componentHidden(ComponentEvent e) {
        }
    }

    private class ListListener implements ListSelectionListener {

        String selection;
        String preTitleSelection;
        String preFavSelection;
        String preTopSelection;
        String titleSelection;
        String favSelection;
        String topSelection;

        public void valueChanged(ListSelectionEvent e) {
            //System.out.println(e);

            titleSelection = (String) movieList.getSelectedValue();
            favSelection = (String) favList.getSelectedValue();
            topSelection = (String) topList.getSelectedValue();

            if (titleSelection != null && !titleSelection.equals(preTitleSelection)) {
                selection = titleSelection;
                preTitleSelection = titleSelection;


            } else if (favSelection != null && !favSelection.equals(preFavSelection)) {
                selection = favSelection;
                preFavSelection = favSelection;

            } else if (topSelection != null && !topSelection.equals(preTopSelection)) {
                selection = topSelection;
                preTopSelection = topSelection;
            }
            /*if (gh.isFavourited()) {
            String[] temp = movies.getTitleListFavourites(movies.getCurrentUserID());


            for (int i = 0; i < temp.length; i++) {
            if (gh.getLastFavourited().equals(temp[i])) {
            m.add(i, temp[i]);
            }
            }
            /*for (int i = 0; i < temp.length; i++) {
            /*if(!m.contains(temp[i])){
            for (int j = 0; j < m.getSize(); j++) {
            if(temp[i].charAt(0)>gh.getLastFavourited().charAt(0) && temp[i].charAt(1)>gh.getLastFavourited().charAt(1)){
            m.add(j, gh.getLastFavourited());
            }

            }
            }

            m.addElement(temp[i]);

            }

            gh.toggleAddFavourite(false);
            }*/

            //
            selectedMovie = movies.getMovieByTitle(selection);
            movies.setSelectedMovie(selectedMovie);
            rightPanel.getUpperPanel().updateIMG(selectedMovie.getImgURL());
            rightPanel.getUpperPanel().getMovieInfo().setText(movies.getMovieInfo(selectedMovie.getMovieID()));
            rightPanel.getUpperPanel().getPlot().setText(movies.getMoviePlot(selectedMovie.getMovieID()));
            rightPanel.getUpperPanel().updateRadioButtons();
            rightPanel.getUpperPanel().getFavBox().setState(gh.isMovieFavourite(movies.getSelectedMovie().getMovieID(), uH.getCurrentUser().getUserID()));
            lP.setTextArea();

        }
    }
}
